package com.happy.ibidding.controller;

import com.happy.ibidding.bean.Ask;
import com.happy.ibidding.bean.Bid;
import com.happy.ibidding.service.AskService;
import com.happy.ibidding.service.BidService;
import com.happy.ibidding.service.CommentService;
import com.happy.ibidding.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("api/ask")
public class AskController {

    final private AskService askService;

    final private BidService bidService;

    final private CommentService commentService;

    @Autowired
    public AskController(AskService askService, BidService bidService, CommentService commentService) {
        this.askService = askService;
        this.bidService = bidService;
        this.commentService = commentService;
    }


    @PostMapping
    public Ask saveAsk(@RequestBody Ask ask) {
        if (Objects.isNull(ask.getId())) {
            ask.setAskId(RandomUtil.generateRandomId("ask-", ""));
            ask.setState("Ready");
        }
        ask.setPublishTime(System.currentTimeMillis());
        return this.askService.save(ask);
    }

    @GetMapping("/all")
    public List<Ask> viewAll() {
        List<Ask> askList = this.askService.viewAll();

        askList.parallelStream().forEach(each -> {
                    each.setBidList(this.bidService.viewAll(each.getAskId(), null));
                    List<Bid> win = each.getBidList().stream().filter(Bid::isWin).collect(Collectors.toList());

                    if (win.size() > 0) {
                        each.setWinBid(win.get(0));
                    }
                }

        );


        return askList;
    }

    @GetMapping("/{askId}")
    public Ask view(@PathVariable String askId) {
        Ask ask = this.askService.view(askId);
        ask.setBidList(this.bidService.viewAll(askId, null));
        ask.setCommentList(this.commentService.viewAll(askId, null));

        List<Bid> win = ask.getBidList().stream().filter(Bid::isWin).collect(Collectors.toList());

        if (win.size() > 0) {
            ask.setWinBid(win.get(0));
        }

        return ask;
    }

    /**
     * ready/cancel/finish
     *
     * @param askId
     * @param state
     * @return
     */
    @PostMapping("/{askId}/state/{state}")
    public Ask changeState(@PathVariable String askId, @PathVariable String state) {
        return this.askService.changeState(askId, state);
    }

    @PostMapping("/{askId}/choose/{bidId}")
    public Ask chooseBid(@PathVariable String askId, @PathVariable String bidId) {
        Ask ask = this.askService.view(askId);
        ask.setState("finish");

        this.askService.save(ask);

        List<Bid> bidList = this.bidService.viewAll(askId, null);

        bidList = bidList.stream().filter(each -> each.getBidId().equals(bidId)).collect(Collectors.toList());

        bidList.parallelStream().forEach(each -> {
            each.setWinTime(System.currentTimeMillis());
            each.setWin(true);
        });

        for (Bid bid : bidList) {
            this.bidService.save(bid);
        }
        return ask;
    }


}
